package services

import "github.com/sirupsen/logrus"

//130	weekSwitch1	//	星期开关１     1字节8个位，1~7位分别代表星期一到星期日
//131	startTime1	//	开启时间1   小时（1字节）
//132		//	开启时间1   分钟（1字节）
//133	endTime1	//	关闭时间1   小时（1字节）
//134		//	关闭时间1   分钟（1字节）
//135	lightMode1	//	灯光模式1    0 正常  1日出日落  2跟随外界
//136	lightBrightness1	//	灯光亮度1   0~100%
//137	lightColorTemperature	//	灯光色温 0~100% 代表 4000K ~10500K的色温
//138	curveColorTemp1	//	曲线色温1
//139	curveColorTemp2	//	曲线色温2
//140	curveColorTemp3	//	曲线色温3
//141	colorTempDemo	//	色温演示
//142	spare1	//	备用
//143	spare2	//	备用
//144	spare3	//	备用
//145	spare4	//	备用
//146	spare5	//	备用
//147	sunriseSetTransDur	//	日出日落渐变时长  分钟
//148	tempLimitLight	//	限光温度
//149	highTempLightLimitSwitch	//	高温限光模式开关
//150	limitBrightness	//	限光亮度
//151	lightControlRegister	//	灯光控制寄存器
// 定义灯光控制器参数映射
var LightCtrlTParamsMap = map[string]DataRule{
	"weekSwitch1":              {StartAddr: 130, Length: 1},
	"startTime1":               {StartAddr: 131, Length: 2}, // 包含小时和分钟
	"endTime1":                 {StartAddr: 133, Length: 2}, // 包含小时和分钟
	"lightMode1":               {StartAddr: 135, Length: 1},
	"lightBrightness1":         {StartAddr: 136, Length: 1},
	"lightColorTemperature":    {StartAddr: 137, Length: 1},
	"curveColorTemp1":          {StartAddr: 138, Length: 1},
	"curveColorTemp2":          {StartAddr: 139, Length: 1},
	"curveColorTemp3":          {StartAddr: 140, Length: 1},
	"colorTempDemo":            {StartAddr: 141, Length: 1},
	"spare1":                   {StartAddr: 142, Length: 1},
	"spare2":                   {StartAddr: 143, Length: 1},
	"spare3":                   {StartAddr: 144, Length: 1},
	"spare4":                   {StartAddr: 145, Length: 1},
	"spare5":                   {StartAddr: 146, Length: 1},
	"sunriseSetTransDur":       {StartAddr: 147, Length: 1},
	"tempLimitLight":           {StartAddr: 148, Length: 1},
	"highTempLightLimitSwitch": {StartAddr: 149, Length: 1},
	"limitBrightness":          {StartAddr: 150, Length: 1},
	"lightControlRegister":     {StartAddr: 151, Length: 1},
}

// 定义灯光控制参数的结构体
type LightCtrlTParams struct {
	WeekSwitch1              uint8  `json:"weekSwitch1"`
	StartTime1               string `json:"startTime1"`
	EndTime1                 string `json:"endTime1"`
	LightMode1               uint8  `json:"lightMode1"`
	LightBrightness1         uint8  `json:"lightBrightness1"`
	LightColorTemperature    uint8  `json:"lightColorTemperature"`
	CurveColorTemp1          uint8  `json:"curveColorTemp1"`
	CurveColorTemp2          uint8  `json:"curveColorTemp2"`
	CurveColorTemp3          uint8  `json:"curveColorTemp3"`
	ColorTempDemo            uint8  `json:"colorTempDemo"`
	Spare1                   uint8  `json:"spare1"`
	Spare2                   uint8  `json:"spare2"`
	Spare3                   uint8  `json:"spare3"`
	Spare4                   uint8  `json:"spare4"`
	Spare5                   uint8  `json:"spare5"`
	SunriseSetTransDur       uint8  `json:"sunriseSetTransDur"`
	TempLimitLight           uint8  `json:"tempLimitLight"`
	HighTempLightLimitSwitch uint8  `json:"highTempLightLimitSwitch"`
	LimitBrightness          uint8  `json:"limitBrightness"`
	LightControlRegister     uint8  `json:"lightControlRegister"`
}

// 事件类型，用于组装命令
type LightCtrlTEvent struct {
	Method string           `json:"method"`
	Params LightCtrlTParams `json:"params"`
}

// 组装并发送命令给设备
func AssembleLightCtrlTPayload(mac string, method string, params map[string]interface{}) error {
	var byteList []ByteData
	for k, v := range params {
		logrus.Info("Key:", k, "Value:", v)
		if dataRule, ok := LightCtrlTParamsMap[k]; ok {
			byteList = append(byteList, ByteData{
				StartAddr: dataRule.StartAddr,
				Length:    dataRule.Length,
				Value:     v,
			})
		}
	}
	logrus.Info("Unsorted byteList:", byteList)
	err := AssembleCommandPayloadByByteData(mac, method, byteList)
	return err
}
